<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   this($)*/

!defined('DYHB_PATH') && exit;

class TemplateGtkGladeWidgetCompiler_Signals extends TemplateGtkGladeWidgetCompilerBase{

	public function compile(TemplateObj $oObject){
		A::INSTANCE($oObject,'TemplateGtkGladeWidget');
		$sCompiled="".$oObject->getCompiled();// 已编译内容
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);// 窗体变量名称
		$sWidgetId=$oObject->getWidgetId();
		$oIterator=$oObject->createSignalIterator();
		$oIterator->first();
		while(!$oIterator->isDone()){
			$arrSignal=$oIterator->current();
			$arrSignal['function']=trim($arrSignal['function']);
			$sConnectFunction='connect';// 连接方式
			if(preg_match('/^@/',$arrSignal['function'])){
				$arrSignal['function']=preg_replace('/^@/','',$arrSignal['function']);
				$sConnectFunction.= '_simple';
			}
			if(preg_match('/^\*/',$arrSignal['function'])){
				$arrSignal['function']=preg_replace('/^\*/','',$arrSignal['function']);
				$sConnectFunction.= '_after';
			}
			$arrHandle=explode('::',$arrSignal['function']);// 回调函数
			foreach($arrHandle as &$sItem){
				if(preg_match('/^[\w_]+$/',$sItem)){
					$sItem="'{$sItem}'";
				}
			}
			$n=count($arrHandle);
			if($n==1){
				if(preg_match('/^\$/',$arrHandle[0])){// _IEventProcessor 对象
					$sHandle="array('EventProcessor','eventTransmitForGtkSignal')";
					$arrSignal['object'].= ($arrSignal['object']?', ':'') . "{$arrSignal['function']}, '{$sWidgetId}-{$arrSignal['name']}'";
				}
				else{// 全局函数
					$sHandle=$arrHandle[0];
				}
			}
			else if($n==2){// 类方法

				$sHandle="array({$arrHandle[0]},{$arrHandle[1]})";
			}
			else{// 异常
				G::E(G::L('意外的 Gtk Signal Handle格式“%s”','Dyhb',null,$arrSignal['function']));
			}
			if($arrSignal['object']){// 参数
				$arrSignal['object']=','.$arrSignal['object'];
			}
			$sCompiled.="\t\t{$sVariableName}->{$sConnectFunction}('{$arrSignal['name']}', {$sHandle} {$arrSignal['object']});";
			$oIterator->next();
		}
		$oObject->setCompiled($sCompiled);
	}

	protected function compileCreateWidget_(TemplateGtkGladeWidget $oObject){}

	protected function compileChildWidgetPacking_(TemplateGtkGladeWidget $oParentWidget,TemplateGtkGladeWidget $oChildWidget,TemplateGtkGladeWidgetPacking $oPacking){}

	static public function regToProcessorManager(ITemplateObjProcessorManager $oManager){
		$oManager->regProcessor('Signals',__CLASS__);
	}

}
